package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 标签业务逻辑处理类
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部标签
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 根据Id查询标签
     * @param Id
     * @return
     */
    public Label findById(String Id){
        return labelDao.findById(Id).get();
    }

    /**
     * 增加标签
     * @param label
     */
    public void add(Label label){
        label.setId(idWorker.nextId()+"");  //设置Id
        labelDao.save(label);
    }

    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }

    /**
     * 删除标签
     * @param Id
     */
    public void deleteById(String Id)
    {
        labelDao.deleteById(Id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Specification<Label> createSpecification(Map searchMap){
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();

                //标签名模糊
                if(searchMap.get("labelname") != null && !"".equals(searchMap.get("labelname"))){
                    predicateList.add(
                            criteriaBuilder.like(
                                    root.get("labelname").as(String.class),"%"+(String)searchMap.get("labelname")+"%"
                            )
                    );
                }

                //状态匹配查询
                if(searchMap.get("state") != null && !"".equals(searchMap.get("state"))){
                    predicateList.add(
                            criteriaBuilder.equal(root.get("state").as(String.class),(String)searchMap.get("state"))
                    );
                }

                //recommend条件
                if(searchMap.get("recommend") != null && !"".equals(searchMap.get("recommend"))){
                    predicateList.add(
                            criteriaBuilder.equal(root.get("recommend").as(String.class),(String)searchMap.get("recommend"))
                    );
                }

                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Label> findSearch(Map searchMap){
        Specification specification = createSpecification(searchMap);
        return labelDao.findAll(specification);
    }


    /**
     * 分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findSearch(Map searchMap,int page,int size){
        Specification specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page, size);
        return labelDao.findAll(specification,pageRequest);
    }


}
